<?php

namespace App\Jobs\Cruise;

use App\Http\Middleware\OpenShop\UserPermissionAuth;
use App\Models\Common\OperateLogModel;
use App\Models\Cruise\CruiseCheckAgainstModel;
use App\Models\OpenShop\Common\OpenShopOrderManagerModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopAddressModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopOrganizationManageUserModel;
use App\Remote\Rms\RmsShopRemote;
use App\Service\Cruise\Check\CruiseCheckAgainstService;
use App\Service\Cruise\Shop\CruiseShopService;
use App\Service\OpenShop\Feishu\MessageSendService;
use App\Service\OpenShop\OrderService;
use App\Service\OpenShop\User\RmsUserService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;

class AgainstExpire implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $timeout = 1800;

    private int $againstId;

    /**
     * 违约函到期
     *
     * @return void
     */
    public function __construct(int $againstId)
    {
        $this->againstId = $againstId;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Log::info("AgainstExpire enter againstId:{$this->againstId}");

        $againstModel = CruiseCheckAgainstModel::query()->find($this->againstId);
        if (!$againstModel) {
            $this->delete();
            return;
        }

        // 判断状态是未处理的违约函
        if ($againstModel->status != CruiseCheckAgainstModel::STATUS_WAIT) {
            $this->delete();
            return;
        }

        Log::info("AgainstExpire doing againstId:{$this->againstId} shopId:{$againstModel->shopId} ");

        /**
         * 4.3 警告函：到期自动确认
         * 5.2 处罚函：到期自动停订货、收银
         */

        if ($againstModel->level == CruiseCheckAgainstModel::LEVEL_WARNING) {//警告
            CruiseCheckAgainstModel::setCheckAgainstInfoById($againstModel->id, ['status' => CruiseCheckAgainstModel::STATUS_CLOSE]);
            // 操作日志
            OperateLogModel::add(
                OperateLogModel::BUSINESS_AGAINST_DETAIL,
                $againstModel->businessId,
                $againstModel->id,
                '到期自动确认',
                '',
                OperateLogModel::STATUS_OK,
                0,
            );
        } elseif ($againstModel->level == CruiseCheckAgainstModel::LEVEL_PUNISH) {//处罚
            // 自动停订货-订货页面有处理 这里就不用处理了

            // 自动停收银
//            CruiseShopService::updateCashRegisterStatusByShopId($againstModel->shopId, RmsShopRemote::SHOP_CASH_REGISTER_STATUS_CHECK_OFF);

            //违约函到期8天后自动停收银
            if (isProd()) {
                $cashOffTime = CruiseCheckAgainstService::END_TIME_DAY_CASH_OFF * 86400;
            } else {
                $cashOffTime = 300;
            }
            dispatch((new AgainstExpireCashOff($this->againstId))->onQueue('{check-expire-cashOff}'))->delay($cashOffTime);


        }

        Log::info("AgainstExpire end againstId:{$this->againstId} shopId:{$againstModel->shopId} ");
    }
}
